#pragma once
#include "PerspectiveCamera.h"
#include "Tracer.h"

class RayTraceMgr
{
public:
    virtual ~RayTraceMgr(void);

    void Init(int nWidth, int nHeight, Vector3f vCameraPos, float fFov);

    void SetAntiAliasing(bool bAntiAliasing)
    {
        m_bAntiAliasing = bAntiAliasing;
    }

	void SetWidthAndHeight(int width, int height);
    void SetLight(const GLfloat *pPosition, const GLfloat *pAmbient, const GLfloat *pDiffuse,
        const GLfloat *pSpecular, const GLfloat fMass);

    // set depth of ray tracing
    void SetRayTraceDepth(int nDepth)
    {
        m_depth = nDepth;
    }

    // Begin calculate the ray tracing.
    void Generate();

    // Just draw the buffer to the screen.
    void Render();

    CPerspectiveCamera* GetCamera()
    {
        return &m_camera;
    }

    static RayTraceMgr* GetInstance()
    {
        if(!m_pInstance)
        {
            m_pInstance = new RayTraceMgr();
        }
        return m_pInstance;
    }

    void ClearBuff()
    {
        if (m_vBuff1)
        {
            delete[] m_vBuff1;
            m_vBuff1 = NULL;
            delete[] m_vBuff2;
            m_vBuff2 = NULL;
            delete[] m_vBuff3;
            m_vBuff3 = NULL;
            delete[] m_vBuff4;
            m_vBuff4 = NULL;
        }
    }


    static RayTraceMgr* m_pInstance;

protected:
    RayTraceMgr(void);
    

    // camera
    CPerspectiveCamera m_camera;

    // scene
    Scene m_scene;

    // tracer
    Tracer m_tracer;

    // if enable anti-aliasing
    bool m_bAntiAliasing;

    // deepth of ray tracing
    int  m_depth;

    // 2D image buffer
    ColorValue *m_vBuff1;
    ColorValue *m_vBuff2;
    ColorValue *m_vBuff3;
    ColorValue *m_vBuff4;

    int m_fWidth, m_fHeight;
    Vector3f m_vLightPos;
    //float m_fvAmbient[4];
    //float m_fvDiffuse[4];
    //float m_fvSpecular[4];
    //float m_fMass;

    // camera
    Vector3f m_vCameraPos;
    float m_fCameraAngle;

};
